package com.shangguan.spring.controller;

import com.shangguan.spring.beans.Code;
import com.shangguan.spring.service.CodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: CodeController
 * @Description:
 * @Author: 一尘
 * @Date: 2020 年 04 月 22 16:18
 * @Version 1.0
 */
@Controller
public class CodeController {
   @Autowired
   CodeService codeService;

    @GetMapping("/list")
    public String list(Model model){
       List<Code> list = codeService.selectAllCode();
          model.addAttribute("list",list) ;
          return  "show"  ;
    }

    @DeleteMapping ("/code/{id}")
    @ResponseBody
    public Map<String,Object> deleteCode(@PathVariable("id") int cid){
        Map<String,Object> map = new HashMap<>();
        int i = codeService.deleteByPrimaryKey(cid);
        if(i>0){
            map.put("msg",cid+"数据删除成功!");
            map.put("success",true);
        }  else {
            map.put("msg","数据删除失败") ;
            map.put("success",false) ;
        }
        return map;
    }


    //数据回显
    @GetMapping("/update/{id}")
    public String update(@PathVariable("id")int cid,Model model){
        Code code = codeService.selectByPrimaryKey(cid);
        model.addAttribute("code",code);
        return "common";
    }
   @PostMapping("/doUpdate")
   public  String doUpdate(Code code){
       int i = codeService.updateByPrimaryKeySelective(code);
       if(i>0){
           return "redirect:list";
       }    else {
           return "error";
       }
   }
}
